package com.sunlg.smarthome.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.sunlg.smarthome.base.App;

/**
 * SharedPreference辅助工具类
 * Created by sunlg on 2017/2/22.
 */

public class PreferHelper {
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;

    public PreferHelper(){
        Context context = App.getInstance();
        preferences = PreferenceManager.getDefaultSharedPreferences(context);
        editor = preferences.edit();
    }

    public PreferHelper(Context context, String whichPre){
        preferences = context.getSharedPreferences(whichPre,context.MODE_PRIVATE);
        editor = preferences.edit();
    }

    public void setString(String key, String value){
        editor.putString(key,value);
        editor.commit();
    }

    public String getString(String key, String defValue){
        return preferences.getString(key, defValue);
    }

    public void setBoolean(String key, boolean value){
        editor.putBoolean(key,value);
        editor.commit();
    }

    public boolean getBoolean(String key, boolean defValue){
        return preferences.getBoolean(key, defValue);
    }

    public void setInt(String key, int value){
        editor.putInt(key, value);
        editor.commit();
    }

    public int getInt(String key, int defValue){
        return preferences.getInt(key, defValue);
    }

    public void clear(){
        editor.clear();
        editor.commit();
    }
}
